package com.buobao.spring.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Aspect
public class MethodChecker {
    @Around("execution(* com.buobao.spring.service..*Service.*())")
    public Object check(ProceedingJoinPoint joinPoint) {
        try {
            long startTime = new Date().getTime();
            Object obj = joinPoint.proceed();//控制目标方法执行
            long endTime = new Date().getTime();
            long duratime = endTime - startTime;
            if (duratime>5) {
                System.out.println("执行时间过长:"+duratime);
            }
            return obj; //将原方法返回
        } catch (Throwable e) {
            throw new RuntimeException(e); //这个异常应该要抛出去，方便后续处理
        }
    }
}
